<?php
if(isset($_SESSION["users_id"]) && $_SESSION["group_id"]==1){
	$IDNews=$_GET["IDNews"];
	settype($IDNews,"int");
	$news=NewsDetail($IDNews);
	$row_news = mysql_fetch_array($news);
	if(isset($_POST["btnEdit"]))
	{
        $Title=$_POST["Title"];
        $Summary=$_POST["Summary"];
        $Image=$_POST["Image"];
        $language_id=$_POST["language"];settype($language_id,"int");
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $datepicker=date('Y-m-d H:i:m');
        $IDUser=$_POST["IDUser"];settype($IDUser,"int");
        $Content=$_POST["Content"];
        $AnHien=$_POST["AnHien"];settype($AnHien,"int");
        EditNews($IDNews,$IDUser,$Title,$Summary,$Content,$Image,$datepicker,$AnHien,$language_id);
		header("location:index.php?p=newslistcp");
	}
?>
    <form id="maincp" name="form1" method="post" action="">
        <h1 class="h1">Add a news</h1>
        <p>Please complete all form below to post a new article.</p>
        <ul class="list2">
            <li><h3>News Title</h3></li>
            <li><input class="input" type="text" name="Title" id="Title" value="<?php echo $row_news['title']; ?>" /></li>
            <li><h3>Summary</h3></li>
            <li><textarea class="textarea" name="Summary" id="Summary" cols="45" rows="5"><?php echo $row_news['summary']; ?></textarea></li>
            <li><h3>News Thumbail</h3></li>
            <li><input class="input twocol" type="text" name="Image" id="Image" value="<?php echo $row_news['image']; ?>" />
                <input class="twocol2" onclick="BrowseServer('','Image')" type="button" name="btnChonFile" id="btnChonFile" value="Browse" />
            </li>
            <li><h3>News Content</h3></li>
            <li><textarea class="ckeditor" name="Content" id="Content" cols="45" rows="5"><?php echo $row_news['content']; ?></textarea></li>
            <li>
                <fieldset>
                    <legend><h3>Options</h3></legend>
                    <p>Show or Hide this article on your page.</p>
                    <label><input <?php if($row_news['hide/show']==0){ echo 'checked="checked"'; } ?> type="radio" name="AnHien" value="0" id="AnHien_0" />Hide</label>
                    <label><input <?php if($row_news['hide/show']==1){ echo 'checked="checked"'; } ?>  type="radio" name="AnHien" value="1" id="AnHien_1" />Show</label>
                </fieldset>
            </li>
            <li>
                <fieldset>
                    <legend><h3>Language</h3></legend>
                    <p>Choose contents language.</p>
                    <select name="language" style="width: 100px">
                        <?php
                        $language=LanguageList();
                        while($row_language=mysql_fetch_array($language)){ ?>
                            <?php if($row_news['language_id']==$row_language['language_id']){ ?>
                            <option selected="selected" value="<?php echo $row_language['language_id']; ?>"><?php echo $row_language['name']; ?></option>
                            <?php }else{ ?>
                            <option value="<?php echo $row_language['language_id']; ?>"><?php echo $row_language['name']; ?></option>
                            <?php } ?>
                        <?php } ?>
                    </select>
                </fieldset>
            </li>
            <li><input class="button addnews" type="submit" name="btnEdit" id="btnEdit" value="Edit News" /></li>
            <input name="IDUser" type="hidden" id="IDUser" value="<?php echo $_SESSION["users_id"];?>" />
        </ul>
        <a href="index.php?p=cpanel" title="Back to Dashboard">&laquo; Back to Dashboard.</a>
    </form>


    <script type="text/javascript">
        function BrowseServer(startupPath, functionData) {
            var finder = new CKFinder();
            finder.basePath = 'js/ckfinder/';
            //???ng path n?i ??t ckfinder
            finder.startupPath = startupPath;
            //???ng path hi?n s?n cho user ch?n file
            finder.selectActionFunction = SetFileField;
            // hàm s? ???c g?i khi 1 file ???c ch?n
            finder.selectActionData = functionData;
            //id c?a text field c?n hi?n ??a ch? hình
            //finder.selectThumbnailActionFunction = ShowThumbnails; //hàm s? ???c g?i khi 1 file thumnail ???c ch?n
            finder.popup();
            // B?t c?a s? CKFinder
        }//BrowseServer

        function SetFileField(fileUrl, data) {
            document.getElementById(data["selectActionData"]).value = fileUrl;
        }

        function ShowThumbnails(fileUrl, data) {
            var sFileName = this.getSelectedFile().name;
            // this = CKFinderAPI
            document.getElementById('thumbnails').innerHTML += '<div class="thumb">' + '<img src="' + fileUrl + '" />' + '<div class="caption">' + '<a href="' + data["fileUrl"] + '" target="_blank">' + sFileName + '</a> (' + data["fileSize"] + 'KB)' + '</div>' + '</div>';
            document.getElementById('preview').style.display = "";
            return false;
            // n?u là true thì ckfinder s? t? ?óng l?i khi 1 file thumnail ???c ch?n
        }
    </script>
<?php
}else{
    header("location:index.php?p=logincp");
}
?>